package com.manor.common.utils

import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.widget.Toast
import androidx.core.content.ContextCompat
import com.orhanobut.logger.Logger
import com.manor.common.activity.AbsActivity
import com.manor.common.bean.UserBean
import com.manor.common.bean.UserNBean
import com.manor.common.presenter.OnChatListener
import com.manor.common.R
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission

/**
 * 封装权限处理


 * @date  2020/8/4  19:55
 * @version 1.0
 */
object ByCallUtil {

    fun convertUserInfo(mBean: UserNBean?): UserBean? {
        if (mBean == null || mBean.basics == null) return null
        Logger.e("mBean ===$mBean")
        val mUserBean = UserBean()
        mUserBean.id = mBean.basics.id
        mUserBean.serial_no = mBean.basics.serial_no
        mUserBean.userNiceName = mBean.basics.user_nickname
        mUserBean.avatar = mBean.basics.avatar
        mUserBean.avatarThumb = mBean.basics.avatar_thumb
        mUserBean.backdropImg = mBean.basics.background
        mUserBean.sex = mBean.basics.sex
        mUserBean.signature = mBean.basics.signature
        mUserBean.birthday = mBean.basics.birthday
        var isAuth = 0
        if (mBean.basics.isauth != "") {
            isAuth = mBean.basics.isauth.toInt()
        }
       // val auth = if (mBean.basics.isAttent == "1") 1 else 0
        mUserBean.hasAuth = if (mBean.basics.isauth == "1") 1 else 0
        mUserBean.auth = mBean.basics.isauth
        mUserBean.isvoice = mBean.basics.isvoice.toInt()
        mUserBean.isvideo = mBean.basics.isvideo.toInt()
        mUserBean.videoPrice = mBean.basics.video_value
        mUserBean.voicePrice = mBean.basics.voice_value
        mUserBean.isdisturb = mBean.basics.isdisturb.toInt()
        return mUserBean
    }


    fun checkPermission(mContext: Activity, listener: OnChatListener) {
        AndPermission.with(mContext)
                .runtime()
                .permission(Permission.READ_EXTERNAL_STORAGE, Permission.WRITE_EXTERNAL_STORAGE, Permission.CAMERA, Permission.RECORD_AUDIO)//
                .onGranted {
                    if (!lacksPermission(mContext, Permission.READ_EXTERNAL_STORAGE)
                            && !lacksPermission(mContext, Permission.WRITE_EXTERNAL_STORAGE)
                            && !lacksPermission(mContext, Permission.CAMERA)
                            && !lacksPermission(mContext, Permission.RECORD_AUDIO)
                    ) {
                        listener.doChatClick()
                    }
                }.onDenied {
                    when {
                        lacksPermission(mContext, Permission.CAMERA) -> {
                            Toast.makeText(mContext, mContext.resources.getString(R.string.permissions_camera), Toast.LENGTH_SHORT).show()
                            if (AndPermission.hasAlwaysDeniedPermission(mContext, it)) {
                                (mContext as AbsActivity).showSettingDialog(mContext, mContext.getResources().getString(R.string.permissions_camera), 10)
                            }
                            return@onDenied
                        }
                        lacksPermission(mContext, Permission.WRITE_EXTERNAL_STORAGE) -> {
                            Toast.makeText(mContext, mContext.resources.getString(R.string.permissions_storage), Toast.LENGTH_SHORT).show()
                            if (AndPermission.hasAlwaysDeniedPermission(mContext, it)) {
                                (mContext as AbsActivity).showSettingDialog(mContext, mContext.getResources().getString(R.string.permissions_storage), 10)
                            }
                            return@onDenied
                        }
                        lacksPermission(mContext, Permission.RECORD_AUDIO) -> {
                            Toast.makeText(mContext, mContext.resources.getString(R.string.permissions_audio), Toast.LENGTH_SHORT).show()
                            if (AndPermission.hasAlwaysDeniedPermission(mContext, it)) {
                                (mContext as AbsActivity).showSettingDialog(mContext, mContext.getResources().getString(R.string.permissions_audio), 10)
                            }
                            return@onDenied
                        }
                    }

                }.start()
    }


    fun lacksPermission(mContexts: Context?, permission: String?): Boolean {
        return ContextCompat.checkSelfPermission(mContexts!!, permission!!) == PackageManager.PERMISSION_DENIED
    }


}